/*
 * FreeModbus Libary: RT-Thread Port
 * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * File: $Id: portserial.c,v 1.60 2013/08/13 15:07:05 Armink $
 */

#include "port.h"
#include "main.h"

/* ----------------------- Modbus includes ----------------------------------*/
#include "mb.h"
#include "mbport.h"

/* ----------------------- static functions ---------------------------------*/
static void prvvUARTTxReadyISR(void);
static void prvvUARTRxISR(void);



extern UART_HandleTypeDef huart1;


/* ----------------------- Start implementation -----------------------------*/
BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
                       eMBParity eParity)
{
	
	MX_USART1_UART_Init(ulBaudRate);
    return TRUE;
}

void vMBPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
{
   ENTER_CRITICAL_SECTION();
		  
  if (xRxEnable)
  {
  	  //使能串口接收中断
	 __HAL_UART_ENABLE_IT(&huart1, USART_IT_RXNE);
  }
  else
  {
     //关闭串口接收中断
	 __HAL_UART_DISABLE_IT(&huart1, USART_IT_RXNE);
  }


  if (xTxEnable)
  {
     //使能串口发送中断
	 __HAL_UART_ENABLE_IT(&huart1, USART_IT_TXE);
  }
  else
  {
 	 //关闭串口发送中断
	 __HAL_UART_DISABLE_IT(&huart1, USART_IT_TXE);
  }
	  
  EXIT_CRITICAL_SECTION();

}

void vMBPortClose(void)
{
	//不做函数处理
}

BOOL xMBPortSerialPutByte(CHAR ucByte)
{ 
	// 发送串口数据 一次发送一个数据
	HAL_UART_Transmit(&huart1, &ucByte,1,5);
    return TRUE;
}

BOOL xMBPortSerialGetByte(CHAR *pucByte)
{
	// 获取串口数据 一次拿一个数据
	HAL_UART_Receive(&huart1,pucByte,1, 0);	
    return TRUE;
}

/*
 * Create an interrupt handler for the transmit buffer empty interrupt
 * (or an equivalent) for your target processor. This function should then
 * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
 * a new character can be sent. The protocol stack will then call
 * xMBPortSerialPutByte( ) to send the character.
 */
void prvvUARTTxReadyISR(void)
{
    pxMBFrameCBTransmitterEmpty();
}

/*
 * Create an interrupt handler for the receive interrupt for your target
 * processor. This function should then call pxMBFrameCBByteReceived( ). The
 * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
 * character.
 */
void prvvUARTRxISR(void)
{
    pxMBFrameCBByteReceived();

}

void USART1_IRQHandler(void)
{
  
  HAL_UART_IRQHandler(&huart1);
  if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) != RESET)
  {    //获取接收RXNE标志位是否被置位
	 prvvUARTRxISR();
	 __HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_RXNE);
	
  } 
  
  if(__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TXE) == SET)
  {
	prvvUARTTxReadyISR();
	__HAL_UART_CLEAR_FLAG(&huart1, UART_FLAG_TXE);
  }
 

}

